//
//  WashMapViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/9/7.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "WashMapViewController.h"

#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
@interface WashMapViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate,BMKPoiSearchDelegate>{
    BMKMapView *mapView;
    BMKLocationService *location;
    BMKGeoCodeSearch *geocodeSearch;
    CGFloat lat;
    CGFloat lng;
    BMKPoiSearch *poisearch;
    NSMutableArray *array;
}

@end

@implementation WashMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBGColor];
    
    [self loadUI];
    array = [NSMutableArray array];
    [self loadMap];
    //[self loadData];
    
}

- (void)loadUI{
    mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [self.view addSubview:mapView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(30, 30, 28, 28);
    [btn setImage:[UIImage imageNamed:@"箭头3"] forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, ScreenHeight-120, ScreenWidth-20, 90)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 90, 70)];
    imageView.image = [UIImage imageNamed:@"timg22"];
    [view addSubview:imageView];
    
    UILabel *labTit = [[UILabel alloc] init];
    labTit.text = @"重庆市南岸白鹤路";
    labTit.font = [UIFont boldSystemFontOfSize:15.0];
    [view addSubview:labTit];
    [labTit makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.right).offset(8);
        make.top.equalTo(imageView.top);
    }];
    
    UILabel *labPlace = [[UILabel alloc] init];
    labPlace.text = @"重庆市南岸白鹤路112号";
    labPlace.textColor = [UIColor darkGrayColor];
    labPlace.font = [UIFont systemFontOfSize:12.0];
    [view addSubview:labPlace];
    [labPlace makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.right).offset(8);
        make.centerY.equalTo(imageView.centerY);
    }];
    
    UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"导航-(1)"]];
    [view addSubview:image2];
    [image2 makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view.right).offset(-8);
        make.bottom.equalTo(imageView.bottom);
        make.width.height.equalTo(20);
    }];
    
    UILabel *labKM = [[UILabel alloc] init];
    labKM.text = @"2.4km";
    labKM.font = [UIFont boldSystemFontOfSize:12.0];
    [view addSubview:labKM];
    [labKM makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(image2.left);
        make.bottom.equalTo(image2.bottom);
    }];
    
    for (NSInteger i = 0; i<5; i++) {
        UIImageView *imageStart = [[UIImageView alloc] initWithFrame:CGRectMake(108+i*13+i+3, 67, 13, 13)];
        imageStart.image = [UIImage imageNamed:@"星-2-拷贝"];
        [view addSubview:imageStart];
    }
    
}

- (void)loadMap{
    //定位服务
    location = [[BMKLocationService alloc] init];
    //开启定位
    [location startUserLocationService];
    
    
    //显示定位图层
    mapView.showsUserLocation = YES;
    //自定义精度圈
    BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
    param.accuracyCircleStrokeColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
    param.accuracyCircleFillColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.3];
    [mapView updateLocationViewWithParam:param];
    
    //反编码
    geocodeSearch = [[BMKGeoCodeSearch alloc] init];
    geocodeSearch.delegate = self;
}

- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [mapView viewWillAppear];
    mapView.delegate = self;
    location.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated{
    [mapView viewWillDisappear];
    mapView.delegate = nil;
    location.delegate = nil;
}

#pragma -mark BMKLocationServiceDelegate
-(void)didUpdateUserHeading:(BMKUserLocation *)userLocation{
    [mapView updateLocationData:userLocation];
    mapView.centerCoordinate = userLocation.location.coordinate;
    //
    
}

-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    [mapView updateLocationData:userLocation];
    mapView.centerCoordinate = userLocation.location.coordinate;
    lat = userLocation.location.coordinate.latitude;
    lng = userLocation.location.coordinate.longitude;
    
    [self loadData];
    //
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    
    reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;
    
    BOOL flag = [geocodeSearch reverseGeoCode:reverseGeocodeSearchOption];
    if(flag){
        NSLog(@"反geo检索发送成功");
        [location stopUserLocationService];
        
    }else{
        NSLog(@"反geo检索发送失败");
    }
    
    //停止定位
    [location stopUserLocationService];
}
#pragma - mark BMKGeoCodeSearchDelegate
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    NSLog(@"%@---%@---%@--",result.addressDetail,result.address,result.businessCircle);
    
}

-(void)didFailToLocateUserWithError:(NSError *)error{
    NSLog(@"定位失败!");
}
//洗车-经纬度
- (void)loadData{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    //29.514623 ,,106.522446
    NSString *longitude = [NSString stringWithFormat:@"%f",lng];
    NSString *latitude = [NSString stringWithFormat:@"%f",lat];
    [parmas setObject:longitude forKey:@"longitude"];
    [parmas setObject:latitude forKey:@"latitude"];
    NSLog(@"parmas:%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/CarWash/graticules",urlHead];
    [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"经纬度res:%@",responseObject);
        array = [responseObject objectForKey:@"result"];
        [self loadAnno];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

- (void)loadAnno{
    
    for (NSDictionary *dic in array) {
        NSString *lats = [dic objectForKey:@"latitude"];
        NSString *lngs = [dic objectForKey:@"longitude"];
        BMKPointAnnotation *anno = [[BMKPointAnnotation alloc] init];
        CGFloat la = [lats floatValue];
        CGFloat lnga = [lngs floatValue];
        anno.coordinate = CLLocationCoordinate2DMake(la, lnga);
        [mapView addAnnotation:anno];
        
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
